// pages/sharebox/addsharemsg/addsharemsg.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    relation: '', //关系
    userNum: '', //号码
    types: 1,
    id: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      types: options.types,
      id: options.id
    })
    if(that.data.types==1){
      that.setData({
        relation: '亲爱的'
      })
    } else if (that.data.types == 2){
      that.setData({
        relation: '父母'
      })
    } else if (that.data.types == 3) {
      that.setData({
        relation: '子女'
      })
    } else if (that.data.types == 4) {
      that.setData({
        relation: '密友'
      })
    } else if (that.data.types == 5) {
      that.setData({
        relation: '其他'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  //输入关系
  inputRelation(e) {
    var that = this;
    that.setData({
      relation: e.detail.value
    })
  },
  //输入号码
  inputNum(e) {
    var that = this;
    that.setData({
      userNum: e.detail.value
    })
  },
  //添加
  addfun() {
    var that = this;
    var relation = that.data.relation;
    var userNum = that.data.userNum;
    if (relation == '') {
      app.showToast('关系不能不为空');
      return false;
    }
    if (userNum == '') {
      app.showToast('账户号不能为空');
      return false;
    }
    app.ajax({
      url: "User/Shared/addShared",
      data: {
        classify: that.data.types,
        relation: relation,
        account: userNum,
        device_id: that.data.id
      },
      success: function(res) {
        if (res.data.code == 1000) {
          app.showToast('添加成功,1秒后自动跳转');
          setTimeout(function() {
            wx.navigateTo({
              url: '../relation/relation?id=' + that.data.id,
            })
          }, 1000)
        }else{
          app.showToast(res.data.message);
        }
      }
    })
  }
})